package com.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.reggie.exception.CustomException;
import com.reggie.pojo.Orders;
import com.reggie.service.OrdersService;
import com.reggie.utils.BaseContext;
import com.reggie.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;

/**
 * @author zhaoyuheng
 * @email 2646498606@qq.com
 * @date 2022/4/28 17:09
 * 订单
 */
@Slf4j
@RestController
@RequestMapping("/order")
public class OrdersController {
    @Autowired
    private OrdersService ordersService;

    /**
     * 提交订单
     *
     * @param orders
     * @return
     */
    @PostMapping("/submit")
    public Result<String> submit(@RequestBody Orders orders) {
        log.info(orders.toString());
        ordersService.submit(orders);
        return Result.success("下单成功");
    }

    /**
     * 展示用户订单信息
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/userPage")
    public Result<Page<Orders>> page(int page, int pageSize) {
        //分页构造器
        Page<Orders> pageInfo = new Page<>(page, pageSize);
        //获取而登录用户
        Long userId = BaseContext.getCurrent();
        //根据id查找订单信息,
        LambdaQueryWrapper<Orders> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Orders::getUserId, userId);
        wrapper.orderByAsc(Orders::getStatus).orderByDesc(Orders::getOrderTime);

        ordersService.page(pageInfo, wrapper);

        return Result.success(pageInfo);
    }

    /**
     * 后台订单页面
     * @param page 页码
     * @param pageSize 每页条数
     * @param number 订单号
     * @param beginTime 订单起始时间
     * @param endTime 订单结束时间
     * @return
     */
    @GetMapping("/page")
    public Result<Page<Orders>> adminPage(int page, int pageSize, Long number, String beginTime, String endTime) {
        //日期解析
        DateTimeFormatter format = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

        LocalDateTime beginTimeFormat;//起始时间
        LocalDateTime endTimeFormat;//结束时间
        Collection<LocalDateTime> times = null;
        //判断是否为空
        if (beginTime != null && endTime != null) {
            beginTimeFormat = LocalDateTime.parse(beginTime, format);
            endTimeFormat = LocalDateTime.parse(endTime, format);
            //将时间包装成一个集合
            times = new ArrayList<>();
            times.add(beginTimeFormat);
            times.add(endTimeFormat);
        }

        //分页构造器
        Page<Orders> pageInfo = new Page<>(page,pageSize);
        //条件构造器
        LambdaQueryWrapper<Orders> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(times != null, Orders::getOrderTime, times);
        wrapper.eq(number != null, Orders::getNumber, number);

        Page<Orders> ordersPage = ordersService.page(pageInfo, wrapper);

        return Result.success(ordersPage);
    }

    /**
     * 更改订单状态
     * @param orders
     * @return
     */
    @PutMapping
    public Result<String> send(@RequestBody Orders orders) {
        log.info(orders.toString());
        Orders ordersById = ordersService.getById(orders.getId());
        //更改状态
        ordersById.setStatus(orders.getStatus());
        //保存数据
        ordersService.updateById(ordersById);

        return Result.success("派送成功");
    }
}
